package com.test.daily.leetcode.y2022.m07.day0701.v04;

/**
 * @author Tom on 2022/7/2
 */
public class Solution {
    public int ways(int[] arr) {
        return process0(arr, 0);
    }

    public static int process0(int[] arr, int index) {
        if (index == arr.length) {
            return isValid(arr) ? 1 : 0;
        }
        if (arr[index] != 0) {
            return process0(arr, index + 1);
        } else {
            int ans = 0;
            for (int i = 1; i < 201; i++) {
                ans += process0(arr, index + 1);
            }
            return ans;
        }
    }

    public static boolean isValid(int[] arr) {
        if (arr[0] > arr[1]) {
            return false;
        }
        if (arr[arr.length - 1] > arr[arr.length - 2]) {
            return false;
        }
        for (int i = 1; i < arr.length - 1; i++) {
            if (arr[i] > Math.max(arr[i - 1], arr[i + 1])) {
                return false;
            }
        }
        return true;
    }

    public static int ways1(int[] arr) {
        int N = arr.length;
        if (arr[N - 1] != 0) {
            return process1(arr, N - 2, arr[N - 2], 2);
        } else {
            int ans = 0;
            for (int i = 0; i < 201; i++) {
                ans += process1(arr, N - 2, i, 2);
            }
            return ans;
        }
    }

    // 我想把 i 位置的数值,设置成 v
    // s == 0, 代表相等
    // s == 2, 代表右大
    // s == 2, 代表右小
    public static int process1(int[] arr, int i, int v, int s) {
        if (i == 0) {
            return ((s == 0 || s == 1) && (v == 0 || arr[0] == v)) ? 1 : 0;
        }
        if (arr[i] != 0 && arr[i] != v) {
            return 0;
        }
        int ans = 0;
        if (s == 1 || s == 0) {
            for (int pre = 1; pre < 201; pre++) {
                ans += process1(arr, i - 1, pre, pre < v ? 0 : (pre == arr[i] ? 1 : 2));
            }
        } else {
            for (int pre = arr[i]; pre < 201; pre++) {
                ans += process1(arr, i - 1, pre, pre == arr[i] ? 1 : 2);
            }
        }
        return ans;
    }
}
